package jin.chen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/stringAndHash")
    public Map<String, Object> testStringAndHash(){
        //redisTemplate默认使用的 是JdkSerializationRedisSerializer这个jdk序列号器
        redisTemplate.opsForValue().set("key1", "value1");
        redisTemplate.opsForValue().set("int_key", "1");
        stringRedisTemplate.opsForValue().set("int", "1");
        //使用运算
        stringRedisTemplate.opsForValue().increment("int", 1);
        //获取底层jedis连接
        Jedis jedis = (Jedis)stringRedisTemplate.getConnectionFactory().getConnection().getNativeConnection();
        //减去1，因为这个命令RedisTemplate不支持，所以先获取底层
        jedis.decr("int");
        Map<String, String> hash = new HashMap<>();
        hash.put("filed1", "value1");
        hash.put("filed2", "value2");
        //存入hash数据类型
        stringRedisTemplate.opsForHash().putAll("hash", hash);
        stringRedisTemplate.opsForHash().put("hash", "f3", "vl3");
        //绑定hash操作的可以，这样就可以连续对同一个散列数据类型就行操作
        BoundHashOperations hashOperations = stringRedisTemplate.boundHashOps("hash");
        //删除两个自段
        hashOperations.delete("filed1", "filed2");
        //新增一个
        hashOperations.put("f4", "vl4");
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @GetMapping("/string")
    public Map<String, Object> string(){
        stringRedisTemplate.opsForValue().set("int", "1");
        //使用运算
        stringRedisTemplate.opsForValue().increment("int", 1);
        //获取底层jedis连接
//        Jedis jedis = (Jedis)stringRedisTemplate.getConnectionFactory().getConnection().getNativeConnection();
        //减去1，因为这个命令RedisTemplate不支持，所以先获取底层
//        jedis.decr("int");
        BoundValueOperations sth = stringRedisTemplate.boundValueOps("int");
        sth.increment(3);
        Map<String, String> hash = new HashMap<>();
        hash.put("filed1", "value1");
        hash.put("filed2", "value2");
        //存入hash数据类型
        stringRedisTemplate.opsForHash().putAll("hash", hash);
        stringRedisTemplate.opsForHash().put("hash", "f3", "vl3");
        //绑定hash操作的可以，这样就可以连续对同一个散列数据类型就行操作
        BoundHashOperations hashOperations = stringRedisTemplate.boundHashOps("hash");
        //删除两个自段
        hashOperations.delete("filed1", "filed2");
        //新增一个
        hashOperations.put("f4", "vl4");
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @GetMapping("/list")
    public Map<String, Object> list(){
        //插入两个链表，顺序不同
        //最右边的数据最开始插入
       stringRedisTemplate.opsForList().leftPushAll("list1", "v2", "v1");
       //最左边的数据开始插入
       stringRedisTemplate.opsForList().rightPushAll("list2", "v1", "v2");
       //绑定list2链表操作
       BoundListOperations listOperations = stringRedisTemplate.boundListOps("list2");
       //从右边弹出一个数据
       Object result1 = listOperations.rightPop();
        System.out.println("result1: -" + result1);
        //获取定位，redis默认从0开始
        Object result2 = listOperations.index(1);
        System.out.println("result2: -" + result2);
        listOperations.leftPush("v0");
        Long result3 = listOperations.size();
        System.out.println("result3: -" + result3);
        List elements = listOperations.range(0, result3-2);
        for(int i = 0; i < elements.size(); i++){
            System.out.println(i + "--" + elements.get(i));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success2", true);
        return map;
    }
}
